<?php

namespace app\admin\controller;

use app\helper\Helper;
use think\Controller;
use think\Session;
use think\Validate;

class Login extends  Controller
{
    /**
     * 登录页面
     * @return \think\response\View
     */
    public function login()
    {
        return view();
    }

    /**
     * 管理员登录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function loginIn()
    {
        $param = $this->request->param();
        $rule = [
            'login_number|登录账号'=>'max:10',
            'password|密码'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            Helper::returnJson(['code'=>500,'msg'=>$validate->getError()]);
        }
        $UserModel = new UserModel();
        $where = [
            'login_number'=>$param['login_number'],
            'password'=>$param['password'],
        ];
        $res = $UserModel->where($where)->find();
        if(!empty($res)){
            Session::set('admin_info',$res);
            Helper::returnJson(['code'=>200,'msg'=>'登录成功']);
        }else{
            Helper::returnJson(['code'=>500,'msg'=>'账号或密码错误']);
        }
    }

    /**
     * 退出登录
     */
    public function loginOut()
    {
        Session::delete('admin_info');
        $this->redirect('login/login');
    }



}